/******************************************************************************
 * Copyright 2022 The Firmament Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

#ifndef DRV_USART_H__
#define DRV_USART_H__

#include <firmament.h>

#ifdef __cplusplus
extern "C" {
#endif

/* 串口 100Kbps 为飞控遥控器的串口通信提供支持 */
#define BAUD_RATE_100000  100000

#if 1 // FMT 旧版本UART配置宏定义
#define SERIAL_DEFAULT_CONFIG_0                   \
    {                                             \
        BAUD_RATE_57600,     /* 57600 bits/s */   \
            DATA_BITS_8,     /* 8 databits */     \
            STOP_BITS_1,     /* 1 stopbit */      \
            PARITY_NONE,     /* No parity  */     \
            BIT_ORDER_LSB,   /* LSB first sent */ \
            NRZ_NORMAL,      /* Normal mode */    \
            SERIAL_RB_BUFSZ, /* Buffer size */    \
            0                                     \
    }

#define SERIAL_DEFAULT_CONFIG_1                   \
    {                                             \
        BAUD_RATE_100000,    /* 100000 bits/s */  \
            DATA_BITS_8,     /* 8 databits */     \
            STOP_BITS_2,     /* 2 stopbit */      \
            PARITY_EVEN,     /* Even parity  */   \
            BIT_ORDER_LSB,   /* LSB first sent */ \
            NRZ_NORMAL,      /* Normal mode */    \
            SERIAL_RB_BUFSZ, /* Buffer size */    \
            0                                     \
    }

#define SERIAL_DEFAULT_CONFIG_2                   \
    {                                             \
        BAUD_RATE_921600,    /* 921600 bits/s */  \
            DATA_BITS_8,     /* 8 databits */     \
            STOP_BITS_1,     /* 1 stopbit */      \
            PARITY_NONE,     /* No parity  */     \
            BIT_ORDER_LSB,   /* LSB first sent */ \
            NRZ_NORMAL,      /* Normal mode */    \
            SERIAL_RB_BUFSZ, /* Buffer size */    \
            0                                     \
    }

#else // 新版本UART配置宏定义
/* Default config for serial_configure structure */
#define RT_SERIAL_CONFIG_DEFAULT_0         \
{                                          \
    BAUD_RATE_57600,  /* 57600 bits/s */   \
    DATA_BITS_8,      /* 8 databits */     \
    STOP_BITS_1,      /* 1 stopbit */      \
    PARITY_NONE,      /* No parity  */     \
    BIT_ORDER_LSB,    /* LSB first sent */ \
    NRZ_NORMAL,       /* Normal mode */    \
    RT_SERIAL_RB_BUFSZ, /* Buffer size */  \
    RT_SERIAL_FLOWCONTROL_NONE, /* Off flowcontrol */ \
    0                                      \
}

#define RT_SERIAL_CONFIG_DEFAULT_1         \
{                                          \
    BAUD_RATE_100000, /* 100000 bits/s */  \
    DATA_BITS_8,      /* 8 databits */     \
    STOP_BITS_2,      /* 2 stopbit */      \
    PARITY_EVEN,      /* Even parity  */   \
    BIT_ORDER_LSB,    /* LSB first sent */ \
    NRZ_NORMAL,       /* Normal mode */    \
    RT_SERIAL_RB_BUFSZ, /* Buffer size */  \
    RT_SERIAL_FLOWCONTROL_NONE, /* Off flowcontrol */ \
    0                                      \
}

/* Default config for serial_configure structure */
#define RT_SERIAL_CONFIG_DEFAULT_2         \
{                                          \
    BAUD_RATE_921600, /* 921600 bits/s */  \
    DATA_BITS_8,      /* 8 databits */     \
    STOP_BITS_1,      /* 1 stopbit */      \
    PARITY_NONE,      /* No parity  */     \
    BIT_ORDER_LSB,    /* LSB first sent */ \
    NRZ_NORMAL,       /* Normal mode */    \
    RT_SERIAL_RB_BUFSZ, /* Buffer size */  \
    RT_SERIAL_FLOWCONTROL_NONE, /* Off flowcontrol */ \
    0                                      \
}
#endif

rt_err_t drv_usart_init(void);

#ifdef __cplusplus
}
#endif

#endif /* DRV_USART_H__ */
